
import java.util.ArrayList;

import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueReceiver;
import javax.jms.Session;
import javax.jms.TemporaryQueue;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.region.TempQueue;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;



/**
 * 
 * @author fei.wu
 * @version 1.0 2011-3-22
 * @since 1.0
 * @see .SendMessage.java
 */
public class SendMessage {
	
	public static void main(String[] args) {
		
		ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("nio://192.168.1.48:61616");
		try {
			Connection connection = factory.createConnection("system","manager");
			connection.start();
			Session s = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Queue queue = new ActiveMQQueue("testQueue");
			
			/*
			 * 单条消息
			TextMessage message = s.createTextMessage();
			message.setText("my message 1");
			MessageProducer p = s.createProducer(queue);
			p.send(message);
			*/
			
			/*一个生成者,多条消费
			MessageProducer p = s.createProducer(queue);
			for (int i = 0; i < 10; i++) {
				p.send(s.createTextMessage("my message "+i));
			}
			*/
			
			/* 订阅
			Topic topic = new ActiveMQTopic("testTopic");
			MessageProducer p = s.createProducer(topic);
			for (int i = 0; i < 10; i++) {
				p.send(s.createTextMessage("my message "+i));
			}
			*/
			
			/*先发送后启消费端
			MessageProducer p = s.createProducer(queue);
			p.setDeliveryMode(DeliveryMode.PERSISTENT);
			p.send(s.createTextMessage("启动消费端后应该收到"));
			
			MessageProducer p1 = s.createProducer(queue);
			p1.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
			p1.send(s.createTextMessage("启动消费端后应该收不到"));
			*/
			
			
			/*
			 * 消息回复*/
			MessageProducer p = s.createProducer(queue);
			TextMessage message = s.createTextMessage();
			message.setText("需要回复的消息");
			Topic topic = new ActiveMQTopic("testTopic");
			message.setJMSReplyTo(topic);
			 p.send(message);
			 
			
			 /*mapMessage 慢
			MessageProducer p = s.createProducer(queue);
			MapMessage m = s.createMapMessage();
			m.setInt("intmessage",101);
			p.send(m);
			*/
		
			/*bytesMessage
			MessageProducer p = s.createProducer(queue);
			BytesMessage m = s.createBytesMessage();
			ArrayList<String> list = new ArrayList<String>();
			list.add("a");
			list.add("b");
			m.writeBytes(Encode.encode(list));
			p.send(m);
			/*
			TemporaryQueue temporaryQueue = s.createTemporaryQueue();
			MessageProducer p = s.createProducer(temporaryQueue);
			p.send(s.createTextMessage("temporaryQueue message"));
			*/
			 p.close();
			 s.close();
			 connection.close();
			 System.out.println("send success");
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}
}
